﻿Imports System.Data.OleDb
Public Class DataProvider
    Shared m_strConnectionString As String = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=..\..\Database\QLTapHoa.mdb"
    Shared m_cConnection As OleDbConnection = Nothing

    Public Shared Function OpenConnection() As Boolean
        Dim isOpen As Boolean = False
        If (m_cConnection Is Nothing) Then
            m_cConnection = New OleDbConnection(m_strConnectionString)
        End If
        m_cConnection.Open()
        If (m_cConnection.State = ConnectionState.Open) Then
            isOpen = True
        End If
        Return isOpen
    End Function

    Public Sub CloseConnection()
        If (m_cConnection IsNot Nothing) Then
            m_cConnection.Close()
        End If
    End Sub

    Public Sub ExecuteNonQuery(ByVal _strSQL As String)
        If (OpenConnection() = True) Then
            Dim command As OleDbCommand = New OleDbCommand(_strSQL, m_cConnection)
            command.ExecuteNonQuery()
            CloseConnection()
        End If
    End Sub

    Public Function ExecuteQuery(ByVal _strSQL As String) As DataTable
        Dim dt As DataTable = New DataTable()
        Dim adapter As OleDbDataAdapter = New OleDbDataAdapter(_strSQL, m_strConnectionString)
        adapter.Fill(dt)
        Return dt
    End Function
End Class
